import cache from '../../api/cache'
import gUtil from '../../mixin/globalUtil'
import {
  request
} from "../../requests/post.js"
const app = getApp();

Page({
  data: {
    photo_url: app.globalData.baseUrl + "/wx/TX.jpg",
    user:{}
  },
  onShow() {
    // if(app.globalData.user !== null && typeof app.globalData.user !== 'undefined')
    // this.setData({"user": app.globalData.user})
    var user = gUtil.getUserInfo();
    console.log(`user info - ${JSON.stringify(user)}`)
    if(user !== null){
      this.setData({"user": user})
    }
    
  },
  onLoad() {
    // 获取基本信息
    // if (user === null || typeof user === 'undefined') {
      
    // } else {
    //   this.setData({
    //     user: user
    //   })
    //   console.log("user " + JSON.stringify(user))
    //   app.globalData.user = user
    //   app.globalData.key = user.key
    // }
  },
  mz(){
    wx.navigateTo({
      url: './mz'
    })
  },
  edit(){
    // console.log("edit " + this.data.user.id)
    if( typeof this.data.user.id === "undefined" ||
      this.data.user.id === '' ||
      this.data.user.id === null){
      this.setData({
        isShowModal: true
      })

      // return
    }

    wx.navigateTo({
      url: './edit'
    })
  },

  about(){
    wx.navigateTo({
      url: './about'
    })
  },
  closeModal(){
    this.setData({
      isShowModal: false
    })
  },

  login(){
    this.setData({
      isShowModal: false
    })
    // 授权登录
    let that = this
    wx.login({
      success(res) {
        if (res.code) {
          request({
            url: '/wx/login',
            code: res.code
          }).then(result => {
            wx.hideLoading()
            if (result.data.errorCode == 200) {
              cache.setOp(app.globalData.userCacheKey, result.data.data)
              app.globalData.key = result.data.data["key"]
              app.globalData.user = result.data.data
              that.setData({
                user: result.data.data
              })
            } else {
              wx.showToast({
                title: result.data.msg,
                icon: 'none',
                duration: 2000,
              })
            }
          }, err => {
            //调用服务端登录接口失败
            wx.hideLoading()
          })
        }
      }
    })
  },
});